package cn.smartorigin.soas.frame;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import cn.smartorigin.soas.data.SqliteConnecter;
import cn.smartorigin.soas.data.TestData;
import cn.smartorigin.soas.data.Time;


public class AddOrder  extends PopupDialogFrame{
	
	private static final long serialVersionUID = 1L;
	//MainFrame mf = null;
	JButton mCfnBtn, mClrBtn;
	JComboBox mClientName, mProductName, mDeliveryNum;
	JTextField mQuality;//, mYear, mMonth, mDay;
	float mTotalPrice;
	
	String mSelectClientName;
	
	public AddOrder(){
		super();
		//mf = mainframe;
		this.setTitle(" 添加清单  - SOAS");
		this.compentInit();
		this.setVisible(true);
	}
	
	private void compentInit(){
		this.setLayout(null);
		BtnListener btnlistener = new BtnListener();
		
		ImageIcon clrCfnIcon=new ImageIcon("img/corfim.png");
		mCfnBtn = new JButton("确定", clrCfnIcon);
		mCfnBtn.setBounds(250,300,100,30);
		mCfnBtn.addActionListener(btnlistener);
		
		ImageIcon clrBtnIcon=new ImageIcon("img/ic_delete.png"); 
		mClrBtn = new JButton("取消",clrBtnIcon);
		mClrBtn.setBounds(450,300,100,30);
		mClrBtn.addActionListener(btnlistener);
		
		/**
		 * 客户名称
		 */
		JLabel clientName  = new JLabel("客户名称：");
		clientName.setBounds(250,25,100,30);
		try {
			mClientName = new JComboBox(TestData.queryClientList(new SqliteConnecter()));
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		mClientName.setBounds(350,25,200,30);
		mClientName.setBackground(Color.white);
		mClientName.addActionListener(btnlistener);
		mSelectClientName = mClientName.getSelectedItem().toString();
		
		/**
		 * 品名
		 */
		JLabel productNamelbl  = new JLabel("品名：");
		productNamelbl.setBounds(250,80,100,30);
		try {
			mProductName = new JComboBox(TestData.queryProductList(new SqliteConnecter(), mSelectClientName));
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		mProductName.setBounds(350,80,200,30);
		mProductName.setBackground(Color.white);
		
		/**
		 * 数量
		 */
		JLabel qualitylbl  = new JLabel("数量/KG：");
		qualitylbl.setBounds(250,135,100,30);
		mQuality = new JTextField();
		mQuality.setBounds(350,135,200,30);
		
//		/**
//		 * 日期
//		 */
//		JLabel datelbl  = new JLabel("日期："); 	datelbl.setBounds(150,190,100,30);
//		JLabel yearlbl  = new JLabel("年"); 		yearlbl.setBounds(310,190,30,30);
//		JLabel monthlbl = new JLabel("月"); 		monthlbl.setBounds(410,190,30,30);
//		JLabel daylbl  = new JLabel("日"); 		daylbl.setBounds(510,190,30,30);
//		mYear = new JTextField();				mYear.setBounds(250,190,50,30); mYear.setHorizontalAlignment(SwingConstants.CENTER);
//		mMonth = new JTextField();				mMonth.setBounds(350,190,50,30);mMonth.setHorizontalAlignment(SwingConstants.CENTER);
//		mDay = new JTextField();				mDay.setBounds(450,190,50,30);  mDay.setHorizontalAlignment(SwingConstants.CENTER);
		
		/**
		 * 订单号
		 */
		JLabel deliverNumlbl  = new JLabel("订单号：");
		deliverNumlbl.setBounds(250,190,100,30);
		mDeliveryNum = new JComboBox();
		mDeliveryNum.setBounds(350,190,100,30);
		mDeliveryNum.setBackground(Color.white);
		
//		int[] bb={1,2,3};
//		int year = Time.getYear();
//		mYear = new JComboBox();
//		mMonth = new JComboBox();
//		mDay = new JComboBox();

//		datelbl.setBounds(150,200,100,30);
//		mYear.setBounds(250, 200, 80, 30);
//		yearlbl.setBounds(340, 200,20,30);
//		mMonth.setBounds(360,200,50,30);
//		monthlbl.setBounds(420, 200, 20, 30);
//		mDay.setBounds(440, 200, 50, 30);
//		daylbl.setBounds(490,200,20,30);


		
		this.add(clientName);
		this.add(mClientName);
		
		this.add(productNamelbl);
		this.add(mProductName);
		
		this.add(qualitylbl);
		this.add(mQuality);
		
		this.add(deliverNumlbl);
		this.add(mDeliveryNum);
		
//		this.add(yearlbl);
//		this.add(monthlbl);
//		this.add(datelbl);
//		this.add(daylbl);
//		this.add(mYear);
//		this.add(mMonth);
//		this.add(mDay);
		
		this.add(mCfnBtn);
		this.add(mClrBtn);
	}
	
	private void reflashProductItem(){
		Vector<Object> v;
		try {
			v = TestData.queryProductList(new SqliteConnecter(), mClientName.getSelectedItem().toString());
			int length = v.size();
			mProductName.removeAllItems();
			for(int i=0;i<length;i++){
				mProductName.addItem(v.get(i));
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}
	
	private void reflashDeliveryNumItem(){
		
		Vector<Object> v;
		try {
			v = TestData.queryDeliveryList(new SqliteConnecter(), mClientName.getSelectedItem().toString());
			int length = v.size();
			mDeliveryNum.removeAllItems();
			for(int i=0;i<length;i++){
				mDeliveryNum.addItem(v.get(i));
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}
	
	private void saveData(){
		String clientName = mClientName.getSelectedItem().toString();
		String productName = mProductName.getSelectedItem().toString();
		String quality = mQuality.getText();
//		String year = mYear.getText();
//		String month = mMonth.getText();
//		String day = mDay.getText();
		String deliveryNum = mDeliveryNum.getSelectedItem().toString();
		int totalPrice;
		if( clientName=="" && productName=="" && quality==""  && deliveryNum==""){
			new ErrorFrame();
		}else{
			try {
				TestData.insertOrder(new SqliteConnecter(), clientName, productName, deliveryNum, quality);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private  void frameClose(){
		this.dispose();
	}
	
	
	class BtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			Object  obj =(Object)e.getSource();
			if(obj == mCfnBtn){
				saveData();
				frameClose();
			}else if(obj == mClrBtn){
				frameClose();
			}else if(obj == mClientName){
				reflashProductItem();
				reflashDeliveryNumItem();
			}
		}
	
	}
	/**
	 * 
	 */
}
